package controllers;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import models.Operator;
import models.Oplog;

import org.apache.commons.lang.StringUtils;

import play.Play;
import services.AuthorityService;
import services.DeviceService;
import services.HraDetailService;
import services.ShopService;
import utils.BizConstants;
import utils.ExcelUtil;
import utils.FileConfig;
import vos.HraDetailSearchVo;
import vos.HraDetailView;
import vos.ReturnCode;
import vos.ReturnListVo;
import vos.ShopSearchVo;
import vos.ShopVo;

public class HraController extends BaseController {
		/**
		 * HRA数据列表页
		 *
		 * @Title: detail 
		 * @return: void
		 */
		public static void detail(String menuid){
			Operator oper=getLoginUser();
			List<String> auths=AuthorityService.getAuthorityByOperAndMenu(menuid,oper.operatorid);
			for(String auth : auths){
				renderArgs.put(auth, auth);
			}
			render();
		}
		/**
		 * 获取列表数据
		 *
		 * @Title: getdetail 
		 * @param page 页
		 * @param rows 页大小
		 * @param idcard 身份证号
		 * @param username 用户名
		 * @param devicename 设备名称
		 * @param shopname 门店名称
		 * @param pagckagename 套餐名称
		 * @return: void
		 */
		public static void getdetail(int page,int rows,String sort,String order, 
				String idcard, String username, String devicename,String devicemodel,
				String shopname,String begintime,String endtime,
				String dealerid,String dealername,
				String shopid,String deviceid,Boolean iscount){
			if(page==0){
				page=1;
			}
			if(rows==0){
				rows=BizConstants.pagesize;
			}
			HraDetailSearchVo vo=new HraDetailSearchVo(page,rows,order,sort, 
					idcard,username,devicename,devicemodel,
					shopname,begintime,endtime,
					dealerid,dealername,shopid,deviceid,iscount);
			long total=HraDetailService.countdetail(vo);
			ReturnListVo listVo=new ReturnListVo();
			listVo.setTotal(total);
			List<HraDetailView> lists=HraDetailService.listHraDetail(vo);
			listVo.setRows(lists);
			renderJSON(listVo);
		}
		/**
		 * 查看详情页
		 *
		 * @Title: view 
		 * @return: void
		 */
		public static void view(String id){
			if(!checkAuthority("HRADATA_VIEW")){
				renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
			}
			HraDetailView detail=HraDetailService.getDetail(id);
			renderArgs.put("detail", detail);
			render();
		}
		/**
		 * 下载pdf
		 *
		 * @Title: getPdf 
		 * @param did HRA数据主键
		 * @return: void
		 */
		public static void getPdf(String did,String deviceid,String idcard){
			if(!checkAuthority("HRADATA_REPORT")){
				renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
			}
			try{
				String pdfName=HraDetailService.getPdfName(did);
				if(StringUtils.isEmpty(pdfName)){
					render("errors/404.html");
				}
				String[] pdfString=pdfName.split("_");
				String dateString=pdfString[1];
				String filePath=BizConstants.filePath+deviceid+"/"+dateString+"/"+idcard+"/"+pdfName;
				File file=Play.getFile(filePath);
				if(!file.exists()){
					renderArgs.put("result", "");
					render("errors/404.html");
				}
				Operator oper=getLoginUser();
				Oplog oplog=new Oplog(oper.operatorid,"下载用户体检报告","体检报告");
				oplog.save(oplog);
				renderBinary(file);
			}catch(Exception e){
				render("errors/404.html");
			}
		}
		/**
		 * 作废（不统计）
		 *
		 * @Title: concleCount 
		 * @param id
		 * @return: void
		 */
		public static void concleCount(String ids){
			if(!checkAuthority("HRADATA_CANCELCOUNT")){
				renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
			}
			Operator oper=getLoginUser();
			if(StringUtils.isEmpty(ids)){
				renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
			}
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			boolean issuccess=HraDetailService.concleCount(listid,oper.operatorid);
			if(issuccess){
				Oplog oplog=new Oplog(oper.operatorid,"取消用户体检统计"+ids,"体检报告");
				oplog.save(oplog);
				renderFailedJson(ReturnCode.SuccessCode,"作废成功");
			}
			else{
				renderFailedJson(ReturnCode.ErrorCode,"作废失败");
			}
		}
		/**
		 * 启用统计
		 *
		 * @Title: concleCount 
		 * @param id
		 * @return: void
		 */
		public static void Count(String ids){
			if(!checkAuthority("HRADATA_COUNT")){
				renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
			}
			Operator oper=getLoginUser();
			if(StringUtils.isEmpty(ids)){
				renderFailedJson(ReturnCode.ErrorCode,"未选中记录");
			}
			String[] id=ids.split(",");
			List<String> listid=new ArrayList<String>();
			Collections.addAll(listid, id);
			boolean issuccess=HraDetailService.Count(listid,oper.operatorid);
			if(issuccess){
				Oplog oplog=new Oplog(oper.operatorid,"启用统计体检报告"+ids,"体检报告");
				oplog.save(oplog);
				renderFailedJson(ReturnCode.SuccessCode,"启用统计成功");
			}
			else{
				renderFailedJson(ReturnCode.ErrorCode,"启用统计失败");
			}
		}
		/**
		 * 导出为excel
		 *
		 * @Title: exportXls 
		 * @param resperson 联系人
		 * @param shopid 门店编号
		 * @param shopname门店名称
		 * @param dealerid 经销商编号
		 * @param dealername 经销商名称
		 * @param isflag 是否停用
		 * @return: void
		 */
		public static void exportXls(String idcard, String username, String devicename,String devicemodel,
				String shopname,String begintime,String endtime,
				String dealerid,String dealername,
				String shopid,String deviceid,Boolean iscount){
			if(!checkAuthority("SHOP_EXPORT")){
				renderFailedJson(ReturnCode.ErrorCode, "您没有操作权限，请联系管理员");
			}
			HraDetailSearchVo vo=new HraDetailSearchVo(
					idcard,username,dealerid,dealername,
					devicemodel,shopid,shopname,devicename,
					begintime,endtime,
					deviceid,iscount);
			List<HraDetailView> lists=HraDetailService.exportXls(vo);
			long y=System.currentTimeMillis();
			String fileName = "体检信息"+y+".csv";
	        response.contentType = "application/x-download";
	        Operator oper =getLoginUser();
	        Oplog oplog=new Oplog(oper.operatorid,"导出体检信息","HRA体检数据");
			oplog.save(oplog);
	        renderBinary(ExcelUtil.buildExportFile(FileConfig.HRAXlsTemplate, lists), fileName);
		}
}
